export const getUserLocation = () => {
  return new Promise((resolve, reject) => {
    // 获取用户信息
    wx.getSetting({
      success({authSetting}) {
        if (authSetting['scope.userLocation']) {
          wx.showLoading({
            title: '正在获取位置...'
          })
          wx.getLocation({
            success (res) {
              wx.hideLoading({
                title: '获取成功'
              })
              wx.setStorageSync('user_location_x', res.latitude)
              wx.setStorageSync('user_location_y', res.longitude)
              console.log('wx.getLocation', res, res.latitude, res.longitude)
              resolve(res)
            },
            fail (res) {
              wx.hideLoading({
                title: '获取失败'
              })
              console.log('拒绝了')
              reject(res)
            }
          })
        } else {
          wx.authorize({scope: "scope.userLocation",
            fail () {
              wx.navigateTo({
                url: '/pages/index/open-setting'
              })
            }
          })
        }
      }
    })
  })
}

export const getDistance = (lat1, lng1, lat2, lng2) => {
  console.log(lat1, lng1, lat2, lng2)
  lat1 = lat1 || 0
  lng1 = lng1 || 0
  lat2 = lat2 || 0
  lng2 = lng2 || 0
  var rad1 = lat1 * Math.PI / 180.0
  var rad2 = lat2 * Math.PI / 180.0
  var a = rad1 - rad2
  var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0
  var r = 6378137
  return (r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))).toFixed(0)
}

